package com.ohs.ohsoa.system.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.ohs.ohsoa.system.dto.InternetHardDrivesFileDto;
import com.ohs.ohsoa.system.service.InternetHardDrivesFileService;
import com.ohs.ohsoa.system.vo.InternetHardDrivesFileVo;
import com.ohs.ohsoa.utils.auth.UserLoginToken;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.bean.responseBean.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 网络硬盘文件
 *
 * @author qianjp
 * @since 2021-10-25
 */
@Api(value = "网络硬盘文件controller", tags = "网络硬盘文件")
@RestController
@RequestMapping(value = "internetharddrivesfile")
public class InternetHardDrivesFileController {
    @Resource
    private InternetHardDrivesFileService internetHardDrivesFileService;

    /**
     * 获取网络硬盘文件
     *
     * @param pageParam 检索用参数
     * @return List<InternetHardDrivesFileVo>
     */
    @UserLoginToken
    @PostMapping(value = "/select/list")
    @ApiOperation(httpMethod = "POST", value = "获取网络硬盘文件")
    public ResultBean<PageInfo<InternetHardDrivesFileVo>> internetHardDrivesFileSelectList(@RequestBody PageParam<InternetHardDrivesFileDto> pageParam) {
        List<InternetHardDrivesFileVo> res = internetHardDrivesFileService.internetHardDrivesFileSelectList(pageParam);
        return ResultBean.success(new PageInfo<>(res));
    }


    /**
     * 新增网络硬盘文件
     *
     * @param internetHardDrivesFileDto 网络硬盘文件数据
     * @return ResultBean<Boolean>
     */
    @UserLoginToken
    @PostMapping(value = "/insertbatch")
    @ApiOperation(httpMethod = "POST", value = "新增网络硬盘文件")
    public ResultBean<Boolean> internetHardDrivesFileInsertList(@RequestBody @Valid InternetHardDrivesFileDto internetHardDrivesFileDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultBean.success(internetHardDrivesFileService.internetHardDrivesFileInsertList(internetHardDrivesFileDto));
    }

    /**
     * 重命名网络硬盘文件
     *
     * @param internetHardDrivesFileDto 网络硬盘文件数据
     * @return ResultBean<Boolean>
     */
    @UserLoginToken
    @PutMapping(value = "/update")
    @ApiOperation(httpMethod = "PUT", value = "重命名网络硬盘文件")
    public ResultBean<Boolean> internetHardDrivesFileUpdateList(@RequestBody @Valid InternetHardDrivesFileDto internetHardDrivesFileDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultBean.success(internetHardDrivesFileService.internetHardDrivesFileUpdateList(internetHardDrivesFileDto));
    }

    /**
     * 删除网络硬盘文件
     *
     * @param internetHardDrivesFileDto 网络硬盘文件数据
     * @return ResultBean<Boolean>
     */
    @UserLoginToken
    @DeleteMapping(value = "/deletebatch")
    @ApiOperation(httpMethod = "DELETE", value = "删除网络硬盘文件")
    public ResultBean<Boolean> internetHardDrivesFileDeleteList(@RequestBody InternetHardDrivesFileDto internetHardDrivesFileDto) {
        return ResultBean.success(internetHardDrivesFileService.internetHardDrivesFileDeleteList(internetHardDrivesFileDto));
    }

    /**
     * 浏览所有网络硬盘文件
     *
     * @param pageParam 检索用参数
     * @return ResultBean<PageInfo < ?>>
     */
    @UserLoginToken
    @PostMapping(value = "/select/all")
    @ApiOperation(httpMethod = "POST", value = "浏览所有网络硬盘文件")
    public ResultBean<PageInfo<InternetHardDrivesFileVo>> internetHardDrivesFileSelectAll(@RequestBody PageParam<InternetHardDrivesFileDto> pageParam) {
        List<InternetHardDrivesFileVo> res = internetHardDrivesFileService.internetHardDrivesFileSelectAll(pageParam);
        return ResultBean.success(new PageInfo<>(res));
    }

    /**
     * 查看网络硬盘文件详细
     *
     * @param internetHardDrivesFileDto 网络硬盘文件数据
     * @param response                  网络硬盘文件详细
     */
    @UserLoginToken
    @PostMapping(value = "/select/detail/down")
    @ApiOperation(httpMethod = "POST", value = "查看网络硬盘文件详细")
    public void internetHardDrivesFileSelectDetail(@RequestBody InternetHardDrivesFileDto internetHardDrivesFileDto, HttpServletResponse response) throws Exception {
        InputStream in = null;
        ServletOutputStream out = null;
        InternetHardDrivesFileVo internetHardDrivesFileVo = internetHardDrivesFileService.internetHardDrivesFileSelectDetail(internetHardDrivesFileDto);

        // 获取文件路径
        String strDownPath = internetHardDrivesFileVo.getIhdfPath();
        // 1.设置文件ContentType类型，会自动判断下载文件类型
        response.setContentType("multipart/form-data");

        // 2.设置文件头：最后一个参数是设置下载文件名(假如我们叫a.pdf)
        response.setHeader("Content-Disposition", "attachment;fileName=" + internetHardDrivesFileVo.getIhdfName());

        File file = new File(strDownPath);

        try {
            int b = 0;
            in = new FileInputStream(file);
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            while ((b = in.read(buffer)) != -1) {
                out.write(buffer, 0, b);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            in.close();
            out.close();
        }

    }

}
